//
//  CaiGouGuiXiaViewController.m
//  tangshantongjiAPPFull
//
//  Created by yuntai on 2018/4/24.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "CaiGouGuiXiaViewController.h"
#import "CommonModel.h"
#import "AddCaiGouGuiXiaViewController.h"
#import "CaiGouGuiXiaDetialViewController.h"

@interface CaiGouGuiXiaViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)NSMutableArray *array;
@property (nonatomic, strong) UITableView *tableview;

@end

@implementation CaiGouGuiXiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationHeight, kScreenBoundWidth, kScreenBoundHeight-kNavigationHeight) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = 134*UIRate;
    tableview.sectionHeaderHeight = 30*UIRate;
    tableview.tableFooterView = [[UIView alloc]init];
    [tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableview = tableview;
    [self.view addSubview:tableview];
    
    BOOL ishow = self.array.count ? NO : YES;
    [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
    [self.tableview reloadData];
    weakself(self)
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakself.currentPage = 1;
        [weakself getdata];
    }];
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakself getdata];
    }];
    

    [self setRightBtnImage:[UIImage imageNamed:@"rightItemAddBg"] eventHandler:^(id sender) {
        AddCaiGouGuiXiaViewController *vc = [[AddCaiGouGuiXiaViewController alloc]init];
        vc.type = weakself.type;
        [UpLoadModelManager sharedInstance].popvc = weakself;
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableview.mj_header beginRefreshing];
}
-(void)dealloc{
    [UpLoadModelManager sharedInstance].popvc = nil;
    [UpLoadModelManager sharedInstance].model = nil;
}
#pragma mark tableviewdelegate datasource
//section的titleHeader
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    CommonModel *model = self.array[section];
    if (section >0) {
        CommonModel *temmodel = self.array[section-1];
        if ([temmodel.add_time isEqualToString:model.add_time]) {
            return @"";
        }
    }
    return model.add_time;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section >0) {
        CommonModel *model = self.array[section];
        CommonModel *temmodel = self.array[section-1];
        if ([temmodel.add_time isEqualToString:model.add_time]) {
            return CGFLOAT_MIN;
        }else{
            return 30*UIRate;
        }
    }else{
        return 30*UIRate;
    }
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // 设置section字体颜色
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:kNavigationColor];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *pricecellid = @"pricevideocellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pricecellid];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:pricecellid];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        UIImageView *yuanview1 = [[UIImageView alloc]initWithFrame:CGRectMake(15*UIRate, 10*UIRate, 80*UIRate, 80*UIRate)];
        yuanview1.layer.cornerRadius = 4;
        yuanview1.layer.masksToBounds = YES;
        yuanview1.tag = 101;
        [yuanview1 setContentMode:UIViewContentModeScaleAspectFill];
        [cell.contentView addSubview:yuanview1];
        
        UIImageView *yuanview2 = [[UIImageView alloc]initWithFrame:CGRectMake(115*UIRate, 10*UIRate, 80*UIRate, 80*UIRate)];
        yuanview2.layer.cornerRadius = 4;
        yuanview2.layer.masksToBounds = YES;
        yuanview2.tag = 102;
        [yuanview2 setContentMode:UIViewContentModeScaleAspectFill];
        [cell.contentView addSubview:yuanview2];
        
        UIImageView *yuanview3 = [[UIImageView alloc]initWithFrame:CGRectMake(215*UIRate, 10*UIRate, 80*UIRate, 80*UIRate)];
        yuanview3.layer.cornerRadius = 4;
        yuanview3.layer.masksToBounds = YES;
        yuanview3.tag = 103;
        [yuanview3 setContentMode:UIViewContentModeScaleAspectFill];
        [cell.contentView addSubview:yuanview3];
        
        UIButton *changeBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenBoundWidth-70*UIRate, 42*UIRate, 50*UIRate, 30*UIRate)];
        changeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        changeBtn.backgroundColor = kNavigationColor;
        changeBtn.layer.cornerRadius = 5.f;
        changeBtn.tag = 104;
        changeBtn.layer.masksToBounds = YES;
        [changeBtn setTitle:@"修改" forState:UIControlStateNormal];
        [cell.contentView addSubview:changeBtn];
        
        
        UIView *yuanview = [[UIView alloc]initWithFrame:CGRectMake(15*UIRate, 113*UIRate, 8*UIRate, 8*UIRate)];
        yuanview.layer.cornerRadius = 4;
        yuanview.layer.masksToBounds = YES;
        yuanview.backgroundColor = kTextDeepDarkColor;
        yuanview.tag = 105;
        [cell.contentView addSubview:yuanview];
        
        UILabel *namel2 = [[UILabel alloc]initWithFrame:CGRectMake(30*UIRate, 100*UIRate, (kScreenBoundWidth-100)*UIRate, 34*UIRate)];
        namel2.tag = 106;
        namel2.textColor = kTextDeepDarkColor;
        namel2.font = [UIFont systemFontOfSize:13];
        [cell.contentView addSubview:namel2];
        
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, (134-0.7)*UIRate, kScreenBoundWidth, 0.7)];
        line.backgroundColor = KSepLineColor;
        [cell.contentView addSubview:line];
    }
    
    CommonModel *model = self.array[indexPath.section];
    
    if (model.img1) {
        UIImageView *iv1 = [cell.contentView viewWithTag:101];
        [ iv1 sd_setImageWithURL:[NSURL URLWithString:model.img1?:@""] placeholderImage:[UIImage imageNamed:@"ic_default_img"]];
    }
    
    if (model.img2) {
        UIImageView *iv2 = [cell.contentView viewWithTag:102];
        [ iv2 sd_setImageWithURL:[NSURL URLWithString:model.img2?:@""] placeholderImage:[UIImage imageNamed:@"ic_default_img"]];

    }
    
    if (self.type == FullGuiXiaGongYe && model.img3) {
        UIImageView *iv3 = [cell.contentView viewWithTag:103];
        iv3.hidden = NO;
        [ iv3 sd_setImageWithURL:[NSURL URLWithString:model.img3?:@""] placeholderImage:[UIImage imageNamed:@"ic_default_img"]];
    }else{
        UIImageView *iv3 = [cell.contentView viewWithTag:103];
        iv3.hidden = YES;
    }

    UIButton *btn = [cell.contentView viewWithTag:104];
    
    UILabel *snl = [cell.contentView viewWithTag:106];
    if ([model.status isEqualToString:@"0"]) {
        snl.text = @"未审核";
        btn.hidden = NO;
    }else if ([model.status isEqualToString:@"1"]){
        btn.hidden = YES;
        snl.text = @"已通过审核";
    }else{
        btn.hidden = NO;
        snl.text = @"审核未通过";
    }


    return cell;
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonModel *model = self.array[indexPath.section];
    CaiGouGuiXiaDetialViewController *vc = [[CaiGouGuiXiaDetialViewController alloc]init];
    if ([model.status isEqualToString:@"0"]) {
        /**可以修改*/
        vc.title = @"修改";
        vc.isCanEdit = YES;
    }else if ([model.status isEqualToString:@"1"]){
        /**不能修改*/
        vc.title = self.type == FullGuiXiaGongYe ? @"规下工业" : @"采购经理";
        vc.isCanEdit = NO;
    }
    vc.type = self.type;
    vc.modelId = model.modelId;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getdata{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    
    NSString *subUrl = nil;
    if(self.type == FullGuiXiaGongYe){
        subUrl = @"api/gxcp/getList.ajax";
        [params setValue:kTokenID forKey:@"token"];
        [params setValue:@(self.currentPage) forKey:@"page"];
        
    }else{
        subUrl = @"api/cgjl/getList.ajax";
        [params setValue:kTokenID forKey:@"token"];
        [params setValue:@(self.currentPage) forKey:@"page"];

    }
    [NetWork PostNetWorkWithUrl:subUrl with:params successBlock:^(NSDictionary *dic) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        NSArray *temparray = nil;
        temparray = dic[@"data"][@"data"];
        if (self.currentPage == 1) {
            [self.array removeAllObjects];
        }
        if (temparray.count<20) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.currentPage ++;
            [self.tableview.mj_footer resetNoMoreData];
        }
        [self.array addObjectsFromArray:[CommonModel mj_objectArrayWithKeyValuesArray:temparray]];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        [self.tableview reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [HUDManager showWarningWithText:msg];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    } errorBlock:^(id error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    }];
    
}

@end
